<?php
/**
 * YICMS
 * ============================================================================
 * 版权所有 2014-2017 YICMS，并保留所有权利。
 * 网站地址: http://www.yicms.vip
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Created by PhpStorm.
 * Author: kenuo
 * Date: 2017/11/17
 * Time: 下午4:38
 */

namespace App\Services\Finance;

use App\Models\Finance\Check;
use App\Models\Finance\CheckRatio;
use App\Repositories\Finance\CheckRepository;
use \Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Auth;

class CheckService
{
    private $_ratioLoop;

    /**
     * ActionLogsService constructor.
     * @param $actionLogsRepository
     */
    public function __construct(CheckRepository $checkRepository)
    {
        $this->_ratioLoop = 2;

        $this->checkRepository = $checkRepository;
    }

    /**
     * 获取 总收入
     */
    public function getIncome($request)
    {
        //总收入
        $totalAmounts = $this->checkRepository->getChannelTotalAmounts($request)->toArray();
        $totalAmounts = array_combine(array_column($totalAmounts, 'key'), $totalAmounts);

        return $totalAmounts;
    }

    /**
     * 获取核查信息
     */
    public function getCheckInfo($request)
    {
        $checkInfo = [];
        $checks = $this->checkRepository->getCheckList($request);

        foreach ($checks as $check) {
            $checkInfo[$check['game_id'] . '-' . $check['channel_id']] = $check;
        }

        return $checkInfo;
    }

    public function create($request)
    {
        $datas = $request->all();

        $check = Check::create($datas);

        for ($i = 0; $i <= $this->_ratioLoop; $i++) {
            if (empty($request->min_value[$i] ||
                $request->self_share_ratio[$i] ||
                $request->channel_share_ratio[$i])) {
                continue;
            }

            $data = [
                'check_id' => $check->id,
                'min_value' => $request->min_value[$i],
                'max_value' => empty($request->max_value[$i]) ? 0 : $request->max_value[$i],
                'self_share_ratio' => $request->self_share_ratio[$i] / 100,
                'channel_share_ratio' => $request->channel_share_ratio[$i] / 100,
            ];
            CheckRatio::create($data);
        }
    }

    public function update($request, $id)
    {
        $datas = $request->all();
        $check = $this->checkRepository->getById($id);
        $check->update($datas);

        CheckRatio::query()->where('check_id', $check->id)->delete();

        for ($i = 0; $i <= $this->_ratioLoop; $i++) {
            if (empty($request->min_value[$i] ||
                $request->self_share_ratio[$i] ||
                $request->channel_share_ratio[$i])) {
                continue;
            }

            $data = [
                'check_id' => $check->id,
                'min_value' => $request->min_value[$i],
                'max_value' => empty($request->max_value[$i]) ? 0 : $request->max_value[$i],
                'self_share_ratio' => $request->self_share_ratio[$i] / 100,
                'channel_share_ratio' => $request->channel_share_ratio[$i] / 100,
            ];
            CheckRatio::create($data);
        }

        return $check;
    }
}